package com.example.plat.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.plat.enity.Activity;
import com.example.plat.enity.Comment;
import com.example.plat.enity.dto.ActivityCardDto;
import com.example.plat.enity.dto.CommentShowDto;
import com.example.plat.exception.CommentException;
import com.example.plat.service.CommentService;
import com.example.plat.util.TimeFormateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.modelmapper.ModelMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(description = "评论管理")
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    ModelMapper mapper;
    @Autowired
    CommentService service;

    @ApiOperation("根据活动ID查询评论")
    @GetMapping("/{activityId}")
    public IPage<CommentShowDto> getComment(@PathVariable Long activityId,
                                            @RequestParam(defaultValue = "0")Integer pageNum,
                                            @RequestParam(defaultValue = "10")Integer pageSize){
        IPage<Comment> page = service.getComment(activityId,pageNum,pageSize);
        return page.convert(u -> {
            CommentShowDto v = new CommentShowDto();
            v.setTime(TimeFormateUtil.conversionTime(u.getTime()));
            BeanUtils.copyProperties(u, v);
            return v;
        });
    }


    @ApiOperation("发布评论")
    @PostMapping
    public ResponseEntity<CommentShowDto> createComment(@RequestBody Comment comment)throws CommentException {
        service.createComment(comment);
        return ResponseEntity.ok(mapper.map(comment,CommentShowDto.class));
    }

    @ApiOperation("删除评论")
    @DeleteMapping
    public ResponseEntity<String> deleteComment(Long commentId)throws CommentException{
        service.deleteComment(commentId);
        return ResponseEntity.ok("删除成功！");
    }
}
